import 'package:enjoy_plus/pages/home/index.dart';
import 'package:enjoy_plus/pages/mine/index.dart';
import 'package:enjoy_plus/utils/eventbus.dart';
import 'package:enjoy_plus/utils/token.dart';
import 'package:flutter/material.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});
  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  // 当前选中的索引
  int currentIndex = 0;
  @override
  void initState() {
    super.initState();
    initToken();
   eventBus.on<LogoutEvent>().listen((event){
    // 页面跳转
    Navigator.pushNamed(context, '/login');
   });
  }
 
  initToken() async{
  await TokenManager().init();
  print(TokenManager().getToken());
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
// 安全区域 SafeArea（手机里）
      body:Container(
        // IndexedStack 索引栈  用于切换页面
        child: IndexedStack(
          index: currentIndex,
           children: [
            const HomePage(),
            MinePage(currentIndex: currentIndex),
           ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        onTap: (int index) {
          setState(() {
            currentIndex = index;
          });
        },
        currentIndex: currentIndex,
        type: BottomNavigationBarType.fixed,
        selectedItemColor: const Color.fromARGB(255, 99, 144, 172),
        unselectedItemColor: Colors.grey,
        items: [
          BottomNavigationBarItem(
            icon: Image.asset(
              'assets/tabs/home_default.png',
              width: 32,
              height: 32,
            ),
            activeIcon: Image.asset(
              'assets/tabs/home_active.png',
              width: 32,
              height: 32,
            ),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              'assets/tabs/my_default.png',
              width: 32,
              height: 32,
            ),
            activeIcon: Image.asset(
              'assets/tabs/my_active.png',
              width: 32,
              height: 32,
            ),
            label: '我的',
          ),
        ],
      ),
    );
  }
}
